package Linux五种IO模型.reactor1;

import java.io.IOException;
import java.nio.channels.*;

/**
 * @author coderFu
 * @create 2022/3/6
 *
 * 处理客户端的链接操作
 */
public class AcceptDemo implements Runnable {

    private final   ServerSocketChannel serverSocketChannel;

    private final   Selector selector;

    public AcceptDemo(ServerSocketChannel serverSocketChannel, Selector selector){

        this.serverSocketChannel = serverSocketChannel;
        this.selector = selector;

    }


    @Override
    public void run() {
        SocketChannel clientChannel = null;
        try {
            clientChannel = serverSocketChannel.accept();
            clientChannel.configureBlocking(false);
            // 注册 客户端的管道事件到选择器中
            SelectionKey register = clientChannel.register(selector, SelectionKey.OP_READ);
            // 给客户端绑定 数据的读取和写入任务类
            register.attach(new HandleDemo(register,clientChannel));
        } catch (IOException e) {
            e.printStackTrace();
        }

        }
    }

